package jm.data.structure.hash;

import jm.data.structure.map.Map;
import jm.data.structure.set.Set;

/**
 * @Description 利用哈希表实现set
 * @date 2022/4/28 17:00
 */
public class HashSet<E> implements Set<E> {

    private HashMap<E,Object> map = new HashMap<>();

    @Override
    public int size() {
        return map.size();
    }

    @Override
    public boolean isEmpty() {
        return map.isEmpty();
    }

    @Override
    public void clear() {
        map.clear();
    }

    @Override
    public boolean contains(E e) {
        return map.containsKey(e);
    }

    @Override
    public void add(E e) {
        map.put(e,null);
    }

    @Override
    public void remove(E e) {
        map.remove(e);
    }

    @Override
    public void traversal(Visitor<E> visitor) {
        map.traversal(new Map.Visitor<E, Object>() {
            @Override
            public boolean visit(E e, Object o) {
                return visitor.visit(e);
            }
        });
    }
}
